package model

import (
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ PlatformModel = (*customPlatformModel)(nil)

type (
	// PlatformModel is an interface to be customized, add more methods here,
	// and implement the added methods in customPlatformModel.
	PlatformModel interface {
		platformModel
		GetOneByName(ctx context.Context,name string)(*Platform,error)
		withSession(session sqlx.Session) PlatformModel
	}

	customPlatformModel struct {
		*defaultPlatformModel
	}
)

// NewPlatformModel returns a model for the database table.
func NewPlatformModel(conn sqlx.SqlConn) PlatformModel {
	return &customPlatformModel{
		defaultPlatformModel: newPlatformModel(conn),
	}
}

func (m *customPlatformModel) withSession(session sqlx.Session) PlatformModel {
	return NewPlatformModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customPlatformModel)GetOneByName(ctx context.Context,name string)(*Platform,error){
	query := fmt.Sprintf("select %s from %s where name = $1 limit 1",platformRows,m.table)
	var resp Platform
	err := m.conn.QueryRowCtx(ctx,&resp,query,name)
	switch err{
	case nil:
		return &resp,nil
	case sqlx.ErrNotFound:
		return nil,ErrNotFound
	default:
		return nil,err 
	}
}
